<template>
  <div>
    <div class="tab-title-box">
      <span class="tab-item">个人资料</span>
    </div>
    <div class="tab-content-box">
      <el-form class="personal-el-form" ref="form" label-width="80px" size="mini" :model="formUser">
        <h2 class="personal-base-header">基本信息</h2>
        <el-form-item label="头像">
          <el-upload
              action=""
              :show-file-list="false"
              :http-request="uploadHttp"
              :before-upload="beforeAvatarUpload"
              accept=".png, .jpg, .jpeg">
            <el-avatar :size="100" :src="formUser.headPortrait || ''" fit="cover">
              <img alt="" src=""/>
            </el-avatar>
          </el-upload>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input
              maxlength="10"
              show-word-limit
              v-model="formUser.nickName"
          ></el-input>
        </el-form-item>
        <el-form-item size="mini">
          <el-button type="primary" @click="onSubmit">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script >
import { mapActions, mapState } from "vuex";
import oss from "@/api/u/oss";
import { updateUserInfo } from "@/api/u/user";
export default {
  data() {
    return {
      formUser: {
        id: 0,
        nickName: "",
        headPortrait: ""
      },
    };
  },
  computed: {
    ...mapState({ userInfo: (state) => state.user }),
  },
  mounted() {
    this.init();
  },
  methods: {
    ...mapActions("user", ["getInfo"]),
    // 初始化
    init() {
      this.getInfo().then((res) => {
        this.formUser = res;
      });
    },
    // 图片限制
    beforeAvatarUpload(file) {
      const isJPEG = file.name.split(".")[1] === "jpeg";
      const isJPG = file.name.split(".")[1] === "jpg";
      const isPNG = file.name.split(".")[1] === "png";
      const isLt500K = file.size / 1024 / 500 < 2;
      if (!isJPG && !isJPEG && !isPNG) {
        this.$message.error("上传图片只能是 JPEG/JPG/PNG 格式!");
      }
      if (!isLt500K) {
        this.$message.error("单张图片大小不能超过 500KB!");
      }
      return (isJPEG || isJPG || isPNG) && isLt500K;
    },
    // 阿里云OSS上传
    uploadHttp(param) {
      const formData = new FormData();
      console.log(param.file);
      formData.append("file", param.file);
      oss.uploadAvater(formData)
          .then((rsp) => {
            console.log("上传图片成功", rsp);
            if (rsp && rsp.success) {
              this.formUser.headPortrait = rsp.data.url;
            }
          })
          .catch((response) => {
            console.log("图片上传失败", response);
          });
    },
    // 更新用户信息
    onSubmit() {
      console.log(this.formUser);
      updateUserInfo(this.formUser).then((res) => {
        if (res.code === 200) {
          this.$message.success(res.message);
          this.init();
        }
      });
    },

  },
};
</script>
<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.tab-title-box {
  display: flex;
  height: 51px;
  line-height: 51px;
  border-bottom: 1px solid #e2e2e2;
  background: #ebeef5;

  .tab-item {
    margin-left: 40px;
    color: $theme-color-font;
    line-height: 51px;
    height: 52px;
    min-width: 65px;
    padding: 0 15px;
    text-align: center;
    cursor: pointer;
    font-size: 18px;
    float: left;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    border-top: 2px solid $theme-color-font;
    background: #fff;
  }
}
.tab-content-box {
  background: #fff;
  padding: 0 30px;
  .personal-base-header {
    font-size: 14px;
    font-weight: 400;
    padding: 0 15px;
    margin-bottom: 30px;
    margin-top: 20px;
  }
  .personal-el-form {
  }
}
</style>